package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.base.converter.PointConverter;
import cn.hellochaos.generator.entity.Point;
import cn.hellochaos.generator.mapper.PointMapper;
import cn.hellochaos.generator.service.PointService;
import cn.hellochaos.generator.vo.PointVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import static cn.hellochaos.generator.entity.dto.LogicFlagCode.DELETE_NOT_RESULT;
import static cn.hellochaos.generator.entity.dto.LogicFlagCode.UPDATE_NOT_RESULT;

/**
* <p>
*  服务实现类
* </p>
*
* @author fmy
* @since 2021-07-16
*/
@Slf4j
@Service
public class PointServiceImpl extends ServiceImpl<PointMapper, Point> implements PointService {
    //注入工具类并使用
    @Autowired
    private PointConverter pointConverter;

    @Override
    public Page<PointVO> listPointsByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询point: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        log.info("正在执行分页查询point:page={}pageSize={}factor={}",page,pageSize,factor);
        //补充字段名精准模糊查询，这里也可以修改一下，尽量不要用模糊查询
        QueryWrapper<Point>queryWrapper=new QueryWrapper<Point>().select(
                "point_id","x_axis","y_axis","radius"
        ).like("point_id",factor);

        //这里只查询部分字段返回依然会导致返回的为空字段
        Page<Point>result=super.page(new Page<>(page,pageSize),queryWrapper);
        //使用工具类并做强制类型转换
        Page<PointVO> resultVO=(Page<PointVO>)pointConverter.toPage(result);

        log.info("分页查询point完毕:结果数={}",resultVO.getRecords().size());
        return resultVO;
    }

    /**
     *
     * @param id 需要查询的Point的id
     * @return 没有数据返回null
     */
    @Override
    public Point getPointById(int id) {
        log.info("正在查询point中id为{}的数据",id);
        Point point = super.getById(id);
        log.info("查询id为{}的point{}",id,(null == point?"无结果":"成功"));
        return point;
    }

    @Override
    public int insertPoint(Point point) {
        log.info("正在插入point");
        if (super.save(point)) {
            log.info("插入point成功,id为{}",point.getPointId());
            return point.getPointId();
        } else {
            log.error("插入point失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deletePointById(int id) {
        log.info("正在删除id为{}的point",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的point成功",id);
            return id;
        } else {
            log.error("删除id为{}的point失败",id);
            //异常可以当做返回值
            //throw new BizException("删除失败[id=" + id + "]");
            return DELETE_NOT_RESULT;
        }
    }

    @Override
    public int updatePoint(Point point) {
        log.info("正在更新id为{}的point",point.getPointId());
        if(super.getById(point.getPointId())==null){
            return UPDATE_NOT_RESULT;
        }
        if (super.updateById(point)) {
            log.info("更新id为{}的point成功",point.getPointId());
            return point.getPointId();
        } else {
            log.error("更新id为{}的point失败",point.getPointId());

            //return DELETE_NOT_RESULT;
            throw new BizException("更新失败[id=" + point.getPointId() + "]");
        }
    }

}
